package com.luxsoft.sw3.replica;

import java.util.Collection;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerListener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;



/**
 * Controlador central de los servicios de replica
 * 
 * @author Ruben Cancino Ramos
 *
 */
public class ReplicaManager {
	
	private static ReplicaManager INSTANCE;
	
	Logger logger=Logger.getLogger(getClass());
	
	private ApplicationContext context;
	
	private ReplicaManager(){		
	}
	
	public static ReplicaManager getInstance(){
		if(INSTANCE==null){
			INSTANCE=new ReplicaManager();
		}
		return INSTANCE;
	}
	
	protected String[] getConfigLocations() {
        return new String[] {                
        		"classpath*:/**/-*.xml"        		
            };
    }
	
	public synchronized ApplicationContext getContext(){		
		if(context==null){
			context=new ClassPathXmlApplicationContext(
					getConfigLocations());
		}
		return context;
	}
	
	protected synchronized void closeContext(){
		((ClassPathXmlApplicationContext)INSTANCE.getContext()).close();
		context=null;
	}
	
	/** Acceso a Replicadores **/
	
	public synchronized void stopReplicador(String groupName){
		try {
			getScheduler().pauseTriggerGroup(groupName);			
		} catch (SchedulerException e) {
			logger.error(ExceptionUtils.getRootCauseMessage(e),e);
		}
		
	}
	
	public synchronized void startReplicador(String groupName){
		try {
			getScheduler().resumeTriggerGroup(groupName);
		} catch (SchedulerException e) {
			logger.error(ExceptionUtils.getRootCauseMessage(e),e);
		}
	}
	
	public synchronized String getTriggers(String triggerName){
		try {
			String[] triggers=getScheduler().getTriggerNames(triggerName);
			return ArrayUtils.toString(triggers);
		} catch (SchedulerException e) {
			logger.error(ExceptionUtils.getRootCauseMessage(e),e);
			return "ND";
		}
	}
	
	
	/** Tareas con el Scheduler ***/
	
	public Scheduler getScheduler(){
		return (Scheduler)context.getBean("replicaScheduler");
	}
	
	
	
	public void addSchedulerListener(SchedulerListener listener){
		try {
			getScheduler().addSchedulerListener(listener);
		} catch (SchedulerException e) {
			logger.error(ExceptionUtils.getRootCauseMessage(e),e);
		}
	}
	
	public void removeSchedulerListener(SchedulerListener listener){
		try {
			getScheduler().removeSchedulerListener(listener);
		} catch (SchedulerException e) {
			logger.error(ExceptionUtils.getRootCauseMessage(e),e);
		}
	}
	
	public void startScheduler() throws SchedulerException{
		getScheduler().start();
		
	}
	
	public void stopScheduler() throws SchedulerException{
		getScheduler().shutdown();
	}
	
	public String[] getJobs() throws SchedulerException{
		return getScheduler().getJobNames(null);
	}
	
	

}
